package com.example.common.utils;

import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;

import com.example.common.interfaces.KeyboardHeightChangeListener;

import java.lang.ref.WeakReference;

/**
 *
 */
public class KeyboardHeightUtils implements ViewTreeObserver.OnGlobalLayoutListener {

    public static final String TAG = KeyboardHeightUtils.class.getSimpleName();
    private int mStatusBarHeight;
    private int mScreenHeight;
    private KeyboardHeightChangeListener mKeyboardHeightChangeListener;
    private View mParentView;
    private Rect mRect;
    private boolean mFirst;
    private int mLastRectHeight;
    private int mErrorHeight;
    private int mLastKeyboardHeight;
    private boolean mSoftInputShowed;

    public KeyboardHeightUtils(View parentView, KeyboardHeightChangeListener listener) {
        this.mParentView = parentView;
        this.mKeyboardHeightChangeListener = new WeakReference<>(listener).get();
        mRect = new Rect();
        mFirst = true;
        mStatusBarHeight = ScreenDimenUtils.getsInstance().getStatusBarHeight();
        mScreenHeight = ScreenDimenUtils.getsInstance().getScreenHeight();
        L.e(TAG, "屏幕高度：---->" + mScreenHeight);
        L.e(TAG, "状态栏高度：---->" + mStatusBarHeight);

    }

    @Override
    public void onGlobalLayout() {
        L.e(TAG, "----onGlobalLayout----");
        if (mParentView != null && mRect != null) {
            mParentView.getWindowVisibleDisplayFrame(mRect);
            int curRectHeight = mRect.height();
            L.e(TAG, "curRectHeight==" + curRectHeight);
            int navigationBarHeight = 0;
            if (NavigationBarUtils.isFullScreen()) {
                navigationBarHeight = NavigationBarUtils.getBottomHeight();
            }
            if (mFirst) {
                mFirst = false;
                mScreenHeight = curRectHeight + mStatusBarHeight + navigationBarHeight-navigationBarHeight;
                mLastRectHeight = curRectHeight;
            }
            if (mLastRectHeight != curRectHeight) {
                L.e(TAG, "mLastCurrentHeight != curRectHeight");
                mLastRectHeight = curRectHeight;
                int visibleHeight = curRectHeight + mStatusBarHeight-navigationBarHeight;
                int keyboardHeight = mScreenHeight - visibleHeight;
                mSoftInputShowed = keyboardHeight > mScreenHeight / 4;
                if (mSoftInputShowed) {
                    keyboardHeight -= mErrorHeight;
                } else {
                    if (keyboardHeight > 0) {
                        mErrorHeight = keyboardHeight;
                    } else {
                        mErrorHeight = 0;
                    }
                    keyboardHeight = 0;
                }
                visibleHeight = mScreenHeight - keyboardHeight;
                if (mLastKeyboardHeight != keyboardHeight) {
                    mLastKeyboardHeight = keyboardHeight;
                    L.e(TAG, "-------可视区高度----->" + visibleHeight);
                    L.e(TAG, "-------键盘高度----->" + keyboardHeight);
                    if (mKeyboardHeightChangeListener != null) {
                        L.e(TAG, "getFullScreenSettingName--" + NavigationBarUtils.getFullScreenSettingName() + "isFullScreen--" + NavigationBarUtils.isFullScreen() + "getBottomHeight--" + NavigationBarUtils.getBottomHeight());
                        mKeyboardHeightChangeListener.onHeightChangedListener(visibleHeight, keyboardHeight);
                    }
                }
            }
        }
    }

    /**
     * 添加布局变化的监听器
     */
    public void start() {
        L.e(TAG, "-------添加键盘监听--->");
        if (mParentView != null) {
            mParentView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        }
    }

    /**
     * 移除布局变化的监听器
     */
    public void release() {
        L.e(TAG, "-------移除键盘监听--->");
        if (mParentView != null) {
            mParentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        mParentView = null;
        mRect = null;
        mKeyboardHeightChangeListener = null;
    }

    /**
     * 是否显示输入法
     *
     * @return
     */
    public boolean isSoftInputShowed() {
        return mSoftInputShowed;
    }

}